package leetcode;

public class Code012_SumRange {

    // 前缀和求法
    public static int sumRange(int[] nums, int left, int right){

        int n = nums.length;
        int[] sums = new int[n + 1];
        sums[0] = 0;
        for(int i = 1; i < n + 1; i++){
            sums[i] = sums[i-1] + nums[i-1];
        }
        return sums[right+1] - sums[left];
    }

    public static void main(String[] args) {
        int[] nums = {-2,0,3,-5,2,-1};
        int res = sumRange(nums, 0,2);
        System.out.println(res);
    }
}
